<!DOCTYPE html>
<title>EventHandlers on SVGAnimationElement test</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
function getObject(interface) {
    switch(interface) {
        case "SVGAnimateElement":
            var e = document.createElementNS("http://www.w3.org/2000/svg", "animate");
            assert_true(e instanceof SVGAnimateElement);
            return e;
        case "SVGAnimateMotionElement":
            var e = document.createElementNS("http://www.w3.org/2000/svg", "animateMotion");
            assert_true(e instanceof SVGAnimateMotionElement);
            return e;
        case "SVGAnimateTransformElement":
            var e = document.createElementNS("http://www.w3.org/2000/svg", "animateTransform");
            assert_true(e instanceof SVGAnimateTransformElement);
            return e;
        case "SVGSetElement":
            var e = document.createElementNS("http://www.w3.org/2000/svg", "set");
            assert_true(e instanceof SVGSetElement);
            return e;
    }
    assert_unreached();
}
function testSet(interface, attribute) {
    test(function() {
        var object = getObject(interface);
        function nop() {}
        assert_equals(object[attribute], null, "Initially null");
        object[attribute] = nop;
        assert_equals(object[attribute], nop, "Return same function");
        object[attribute] = "";
        assert_equals(object[attribute], null, "Return null after setting string");
        object[attribute] = null;
        assert_equals(object[attribute], null, "Finally null");
    }, "Set " + interface + "." + attribute);
}
function testReflect(interface, attribute) {
    test(function() {
        var element = getObject(interface);
        assert_false(element.hasAttribute(attribute), "Initially missing");
        element.setAttribute(attribute, "return");
        assert_equals(element.getAttribute(attribute), "return", "Return same string");
        assert_equals(typeof element[attribute], "function", "Convert to function");
        element.removeAttribute(attribute);
    }, "Reflect " + interface + "." + attribute);
}
// Object.propertyIsEnumerable cannot be used because it doesn't
// work with properties inherited through the prototype chain.
function getEnumerable(interface) {
    var enumerable = {};
    for (var attribute in getObject(interface)) {
        enumerable[attribute] = true;
    }
    return enumerable;
}
function testEventHandlerMapping(attribute, eventname) {
    async_test(function(t) {
        var element = getObject("SVGAnimateElement");
        assert_false(element.hasAttribute(attribute), "Initially missing");
        element[attribute] = function() {
            t.step(function (){assert_true(true); t.done();});
        };
        var event = new CustomEvent(eventname);
        element.dispatchEvent(event);
    }, "Event " + eventname + " maps to " + attribute);
}
var enumerableCache = {};
function testEnumerate(interface, attribute) {
    if (!(interface in enumerableCache)) {
        enumerableCache[interface] = getEnumerable(interface);
    }
    test(function() {
        assert_true(enumerableCache[interface][attribute]);
    }, "Enumerate " + interface + "." + attribute);
}

var attributeToEventMap = {
    "onbegin" : "beginEvent",
    "onend" : "endEvent",
    "onrepeat" : "repeatEvent"
};

for (var attribute in attributeToEventMap) {
    [
        "SVGAnimateElement",
        "SVGAnimateMotionElement",
        "SVGAnimateTransformElement",
        "SVGSetElement"
    ].forEach(function(interface) {
        testSet(interface, attribute);
        testEnumerate(interface, attribute);
        testReflect(interface, attribute);
    });

    testEventHandlerMapping(attribute, attributeToEventMap[attribute]);
}
</script>
